#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <unordered_map>
using namespace std;
class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k)
    {
        unordered_map<int, int> um;
        for (int i = 0;i < nums.size();i++)
        {
            if (um.find(nums[i]) == um.end())
            {
                um[nums[i]] = i;
            }
            else
            {
                //cout<<i<<" "<<um[nums[i]]<<endl;
                if (i - um[nums[i]] <= k) return true;
                else um[nums[i]] = i;
            }
        }
        return false;
    }
};